/*mui.init({
	pullRefresh: {
		container: '#pullrefresh',
		down: { //下拉刷新
			callback: pulldownRefresh,
			style: mui.os.android ? "circle" : "default"
		},
		up: {
			contentinit: '',
			contentrefresh: '正在加载...',
			contentnomore: '没有更多了',
			callback: pullupRefresh
		}
	}
})

// mui.previewImage();

var commentType = 0, //0为帖子评论，1为单条评论
	timeout = '',
	index = 0,
	topicid = '', //帖子id
	page = 1, //初始页码
	pageCount = 0, //总页数
	author = '', //作者
	bbsType = 0, //0为全部评论，1为只看作者
	floor = 0, //帖子楼层
	pid = 0;

mui.plusReady(function() {

//	var self = plus.webview.currentWebview();
//	topicid = self.info.id;

});

//下拉刷新具体业务实现
function pulldownRefresh() {
	setTimeout(function() {
		bbsType = 0;
		getDatails();
		//下拉刷新结束
		mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
		//重置上拉加载
		mui('#pullrefresh').pullRefresh().refresh(true);
		//重置页码
		page = 1;
	}, 1500)
}

// 上拉加载具体业务实现
function pullupRefresh() {
	if(pageCount > page) {
		setTimeout(function() {
			if(bbsType == 1) {
				request('/Topic/getTopicDetail', {
					topicid: topicid,
					pageindex: page + 1,
					authorid: author
				}, function(r) {
					log(r);
					pageCount = r.countpage;
					author = r.data.topicdetail.uid;
					r.data.topicdetail.content = HTMLDecode(r.data.topicdetail.content);
					//注入楼层
					for(var i = 0; i < r.data.commentlist.length; i++) {
						floor++;
						r.data.commentlist[i].floor = floor;
					}
					r.data.bbsType = bbsType;
					render('#bbsDetail', 'bbsDetailTep1', r.data, true);
					appPage.imgInit();
					document.getElementById("commentNum").innerHTML = '评论 ' + r.data.commentlist.length;
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
				}, false, function() {
					appPage.endPullRefresh(true);
				});
			} else {
				request('/Topic/getTopicDetail', {
					topicid: topicid,
					pageindex: page + 1
				}, function(r) {
					log(r);
					pageCount = r.countpage;
					author = r.data.topicdetail.uid;
					r.data.topicdetail.content = HTMLDecode(r.data.topicdetail.content);
					//注入楼层
					for(var i = 0; i < r.data.commentlist.length; i++) {
						floor++;
						r.data.commentlist[i].floor = floor;
					}
					r.data.bbsType = bbsType;
					render('#bbsDetail', 'bbsDetailTep1', r.data, true);
					appPage.imgInit();
					document.getElementById("commentNum").innerHTML = '评论 ' + r.data.commentlist.length;
					mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
				}, false, function() {
					appPage.endPullRefresh(true);
				});
			}
			page++;
		}, 1500);
	} else {
		//mui.toast("温馨提示：没有更多数据");
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
	}

}

function HTMLDecode(text) {
	var temp = document.createElement("div");
	temp.innerHTML = text;
	var output = temp.innerText || temp.textContent;
	temp = null;
	return output;
}

function goToWhere(where) {
	var me = this;
	me.site = [];
	me.sleep = me.sleep ? me.sleep : 16;
	me.fx = me.fx ? me.fx : 6;
	clearInterval(me.interval);
	var dh = document.documentElement.scrollHeight || document.body.scrollHeight;
	var height = !!where ? dh : 0;
	me.interval = setInterval(function() {
		var top = document.documentElement.scrollTop || document.body.scrollTop;
		var speed = (height - top) / me.fx;
		if(speed === me.site[0]) {
			window.scrollTo(0, height);
			clearInterval(me.interval);
		}
		window.scrollBy(0, speed);
		me.site.unshift(speed);
	}, me.sleep);
};
*/
var inspectUnitId,userId,roleType;
var status = 0;
mui.init();


mui.plusReady(function(){

	self = plus.webview.currentWebview();
	inspectUnitId = self.info.inspectUnitId;
	
	userId = localStorage.getItem("userId");
	roleType = localStorage.getItem("roleType");

	var roleType = localStorage.getItem("roleType");
	//document.getElementById('jumpToScane').style.display = "block";
	if(roleType==2){
		document.getElementById('jumpToScane').style.display = "block";
	}
	//跳转扫码
	document.getElementById('jumpToScane').addEventListener('tap', function() {
		openNew("../widget/scancode.html", {
			planId: inspectUnitId
		});
		//openNew("../../html/tour/checkReport.html");
	})
	document.getElementById("selectType").addEventListener('tap', function() {
		var btnArray = [{
			title: "全部"
		}, {
			title: "未完成"
		}, {
			title: '完好'
		}, {
			title: '有问题'
		}];
		plus.nativeUI.actionSheet({
			title: "选择类型",
			cancel: "取消",
			buttons: btnArray
		}, function(e) {
			var index = e.index;
			status = index-1;
			document.getElementById('list_wrap').innerHTML = '';
			loadData();
		});
	});

	loadData()
	
})

window.addEventListener('refresh', function(e){//执行刷新
	location.reload();
});
function loadData() {
	request("/app/alarm/site/list/"+inspectUnitId+"/"+userId+"/"+status, "GET",{
		// id: userId,
		// unitId: inspectUnitId,
		// roleType: roleType,
		// pageNum: 1,
		// pageSize: 20
	}, function(res) {
		render('#list_wrap', 'taskDetailList', res);
		mui('#list_wrap').on('tap', '.task-li', function(e) {
			var tskId = this.getAttribute("data-id");
			var siteId = this.getAttribute("data-siteId");
			//console.log(tskId)
			openNew("checkReportDetail.html", {
				taskId: tskId,
				siteId: siteId
			});
		})
//		appPage.endPullRefresh();
// 		if (roleType == 1 || roleType == 2){
// 			render('#list_wrap', 'taskList', res);
// 			mui('.handle').on('tap', 'button', function(e) {
// //				alert(this.getAttribute("data-id"));
// 				var tskId = this.getAttribute("data-id");
// 				openNew("checkReport.html", {
// 					taskId: tskId,
// 					inspectUserId: res.result.inspectUserId,
// 					type: res.result.type
// 				});
// 			})
// 		}else {
// 			render('#list_wrap', 'taskDetailList', res);
// 			mui('.deal').on('tap', 'button', function(e) {
// //				alert(this.getAttribute("data-id"));
// 				var tskId = this.getAttribute("data-id");
// 				openNew("checkReportDetail.html", {
// 					taskId: tskId
// 				});
// 			})
// 		}
		
	}, true, function() {
//		appPage.endPullRefresh();
	});
}
